package com.wkw.jkyf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wkw.jkyf.common.ResultMapUtil;
import com.wkw.jkyf.pojo.Druginfo;
import com.wkw.jkyf.pojo.Owinfo;
import com.wkw.jkyf.pojo.Supplier;
import com.wkw.jkyf.service.IDruginfoService;
import com.wkw.jkyf.service.IOwinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/owinfo")
public class OwinfoController {
    /*
转向供应商页面
*/
    @Resource
    private IOwinfoService owinfoService;

    @RequestMapping
    public String login(){
        return "/owinfo";
    }


    //分页查询登记出入库列表
    @RequestMapping("/owinfoQueryPage")
    @ResponseBody
    public Object supplierQueryPage(String param, @RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int pageSize){
        try{
            IPage<Owinfo> iPage = owinfoService.selectOwinfoPage(page,pageSize,param);
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        }catch (Exception e){
            return ResultMapUtil.getHashException(e);
        }
    }
    //跳转到添加页面
    @RequestMapping(value = "/owinfoPage")
    public String supplierPage(){
        return "/owinfoPage";
    }

    //添加
    @RequestMapping(value = "/owinfoAdd")
    @ResponseBody
    public Object druginfoAdd(Owinfo owinfo){
        try {
            owinfo.setCreatetime(new Date());
            int i = owinfoService.addOwinfo(owinfo);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashException(e);
        }

    }

    //按id查询
    @RequestMapping(value = "/owinfoQueryById")
    public String owinfoQueryById(@RequestParam(name = "id",required = true)Integer id, Model model) {
        Owinfo owinfo= owinfoService.queryOwinfofoById(id);
        model.addAttribute("obj",owinfo);
        return "/owinfoPage";
    }

    //修改一个供应商
    @RequestMapping(value = "/owinfoEdit")
    @ResponseBody
    public Object owinfoEdit(Owinfo owinfo) {
        try {
            int i= owinfoService.editOwinfo(owinfo);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return  ResultMapUtil.getHashException(e);
        }
    }

    // 删除一个供应商
    @RequestMapping(value = "/owinfoDelById")
    @ResponseBody
    public Object owinfoDelById(Integer id) {
        try {
            int i= owinfoService.deleteOwinfoById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return  ResultMapUtil.getHashException(e);
        }
    }



}
